<?php


namespace App\Http\Controllers;

use App\Common\Libs\SystemCode;
use App\Common\Libs\SystemMessage;
use App\Http\Controllers\Wx\WxController;
use App\Models\Mysql\Address;
use App\Services\AddressService;
use App\Services\User\UserService;
use Illuminate\Support\Str;

class AddressController extends WxController
{
    protected $userService;
    protected $param;
    protected $only = ['index'];

    //protected $except = ['login','register'];
    public function __construct()
    {
        parent::__construct();
        $this->userService = UserService::getInstance();
        $this->param = $this->request->input();
    }

    /**
     * 获取用户地址列表
     * @return mixed
     */
    public function list()
    {
        $id = $this->user()->id ?? 1;
        $list = AddressService::getInstance()->getAddressListByUserId($id);
        //解决驼峰法 将数据库字段自动转换为驼峰法
        /*$list = $list->map(function (Address $address){
            $address->toArray();
            $item = [];
            foreach ($address as $key=>$value){
                $key = lcfirst(Str::studly($key)); //自动转换成驼峰命名
                $item[$key] = $value;
            }
            return $item;
        });*/
        return $this->successPaginate($list);
        /*return $this->success([
            'total' => $list->count(),
            'page' => 1,
            'list' => $list->toArray(),
            'pages' => 1,
            'limit' => $list->count(),
        ]);*/
    }

    public function save()
    {
    }

    public function delete(\Illuminate\Http\Request $request)
    {
        $id = $request->input('id', 0);
        if (!$id && !is_numeric($id)) {
            return show(SystemMessage::PARAM_NOT_FOUND, SystemCode::PARAM_NOT_FOUND);
        }
        AddressService::getInstance()->delete($this->user()->id, $id);
        return $this->success();
    }


}
